/**
 * 
 */
package com.arsl.client;

import com.arsl.client.data.PageView;
import com.arsl.client.data.TagView;
import com.arsl.client.util.Utils;
import com.arsl.client.view.ViewPageButton;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author nilu
 * 
 */
public class ViewPage implements EntryPoint {

	String url;
	AbsolutePanel panel = new AbsolutePanel();
	Button btn = new Button();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	@Override
	public void onModuleLoad() {
		final String arPageId = Window.Location.getParameter("arPageId");
		String auth = Window.Location.getParameter("auth");
		PaperaziServiceAsync rpcService = GWT.create(PaperaziService.class);

		// required args not passed
		if (arPageId == null || auth == null) {
			return;
		}

		AsyncCallback<Void> callback = new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};

		rpcService.incHits(Integer.parseInt(arPageId), callback);
		rpcService.viewPage(Integer.parseInt(arPageId), auth,
				new AsyncCallback<PageView>() {

					@Override
					public void onSuccess(PageView result) {
						// Window.alert(result.getPageImageUrl());
						setPage(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Failed to load!");
						caught.printStackTrace();
					}
				});

	}

	private void setPage(PageView result) {
		url = result.getPageImageUrl();
		Image image = new Image(url);
		float[] dimensions = Utils.getresizedDimensions(result.getPaper()
				.getHeight(), result.getPaper().getWidth(), Window
				.getClientWidth());
		int imgWidth = (int) dimensions[0];
		int imgHeight = (int) dimensions[1];

		image.setPixelSize(imgWidth, imgHeight);

		panel.add(image);
		for (TagView tg : result.getTagViews()) {
			ViewPageButton btn = new ViewPageButton();
			btn.setContentList(tg.getContent());
			btn.setImgWidth(imgWidth);
			btn.setImgHeight(imgHeight);
			btn.setStyleName("customButton");

			int x = (int) (tg.getTag().getStartx() * imgWidth / result
					.getPaper().getWidth());
			int w = (int) (tg.getTag().getWidth() * imgWidth / result
					.getPaper().getWidth());
			int y = (int) (tg.getTag().getStarty() * imgHeight / result
					.getPaper().getHeight());
			int h = (int) (tg.getTag().getHeight() * imgHeight / result
					.getPaper().getHeight());

			// btn.setSize(String.valueOf(tg.getTag().getWidth()) + "px",
			// String.valueOf(tg.getTag().getHeight()) + "px");
			btn.setSize(w + "px", h + "px");

			panel.add(btn, x, y);
			Image pImg = new Image();
			pImg.setUrl("http://www.paperazi.com/img/pp.png");
			pImg.setWidth(50 + "px");
			pImg.setHeight(50 + "px");
			panel.add(pImg, x + w - 35, y + h - 35);
		}
		RootPanel.get().add(panel);
	}
}
